package scrollimage.image;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sosee.cgfw.R;
import com.sosee.cgfw.activity.AboutUsActivity;
import com.sosee.cgfw.activity.ImgActivity;
import com.lidroid.xutils.BitmapUtils;
import com.sosee.cgfw.activity.WebActivity;
import com.sosee.cgfw.utils.Constans;
import com.sosee.cgfw.utils.Utils;


import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import scrollimage.common.ScrollImageEntity;
import scrollimage.utils.Convert;

/**
 *
 */
@SuppressWarnings("ResourceType")
public class ScrollImageController {
    private Activity activity;
    private BitmapUtils bitmapUtils;

    public ScrollImageController(Activity activity) {
        this.activity = activity;
        bitmapUtils = new BitmapUtils(activity);
    }

    /*
     *
     * @param list
     * @param imgScroll
     * @param ovalLayout
     */
    public void initViewPagerImage(List<ScrollImageEntity> list, ImageScroll imgScroll,
                                   LinearLayout ovalLayout,boolean isAd) {
        List<View> listViewsImage = new ArrayList<View>();


        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {

                // 新建布局
                RelativeLayout relativeLayout = new RelativeLayout(activity);
                // 新建图片展示
                final ImageView imageView = new ImageView(activity);
                // 新建图片标题
                TextView textView = new TextView(activity);
                imageView.setId(1);
                textView.setId(2);
                RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT);
                lp1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                lp1.addRule(RelativeLayout.CENTER_HORIZONTAL,
                        RelativeLayout.TRUE);

                RelativeLayout.LayoutParams lp2;
                if(isAd){
                    String url=list.get(i).getImageUrl();
                    URL u=null;
                    try {
                         u=new URL(url);
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    }
                    Utils.onLoadImage(u, new Utils.OnLoadImageListener() {
                        @Override
                        public void OnLoadImage(Bitmap bitmap, String bitmapPath) {
                            imageView.setImageBitmap(bitmap);
                        }
                    });
                    String title=list.get(i).getTitle();
                    if(title!=null&&title.length()>3){
//                        bitmapUtils.display(imageView, list.get(i).getImageUrl());
                        textView.setTextColor(android.graphics.Color.WHITE);
                        textView.setTextSize(13);
                        textView.setBackgroundColor(android.graphics.Color.BLACK);
                        textView.getBackground().setAlpha(100);
                        textView.setGravity(Gravity.CENTER_VERTICAL);
                        textView.setText("   " + list.get(i).getTitle() + "   ");
                    }
//
                }else{
                    imageView.setImageResource(R.drawable.homeindexbanner);
                }
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                relativeLayout.addView(imageView, lp1);
                if (list.get(i).getTitle() .getBytes().length > 70) {
                    lp2 = new RelativeLayout.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT,
                            Convert.dip2px(activity, 38));
                    lp2.addRule(RelativeLayout.BELOW, 1);
                    lp2.setMargins(0, Convert.dip2px(activity, -38), 0, 0);
                } else {
                    lp2 = new RelativeLayout.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT,
                            Convert.dip2px(activity, 28));
                    lp2.addRule(RelativeLayout.BELOW, 1);
                    lp2.setMargins(0, Convert.dip2px(activity, -28), 0, 0);
                }
                relativeLayout.addView(textView, lp2);
                listViewsImage.add(relativeLayout);
                //为测试点击事件，这里传入名字，可根据实际情况设置
                imageView.setOnClickListener(new ImageOnclickListener(list.get(i).getUrl()));
        }
        }
        imgScroll.start(activity, listViewsImage, 4000, ovalLayout,
                R.layout.ad_bottom_item, R.id.ad_item_v,
                R.drawable.dot_focused, R.drawable.dot_normal, true, true);
    }
/**
 * 滚动图片点击事件，此类构造方法可直接传入对象，取对象中的数据供业务逻辑使用
 */
        class ImageOnclickListener implements View.OnClickListener {
            String name;

            ImageOnclickListener() {
            }

            ImageOnclickListener(String name) {
                this.name = name;
            }

            @Override
            public void onClick(View view) {
                if(name!=null&&name.length()>0){
                    Intent intent=new Intent(activity,WebActivity.class);
                    intent.putExtra("url","http://"+name);
                    activity.startActivity(intent);
                }else{
                }
            }
        }
}
